/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.bookingmanager.adapters.impl;

import com.sun.xml.bind.v2.model.core.Adapter;
import cz.muni.fi.pa165.bookingmanager.dto.*;
import cz.muni.fi.pa165.bookingmanager.entities.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jiří Holuša
 */
public class AdaptersHelper {
    
    
    /**
     * Transfers entity to DTO object
     * 
     * @param customerEntity
     * @return 
     */
    public static CustomerDTO customerEntityToDTO(Customer customerEntity){
        CustomerDTO customerDTO = new CustomerDTO();
        
        customerDTO.setId(customerEntity.getId());
        customerDTO.setFirstName(customerEntity.getFirstName());
        customerDTO.setSurName(customerEntity.getSurName());
        customerDTO.setNumberOfIdCard(customerEntity.getNumberOfIdCard());
        
        return customerDTO;
    }
    
    /**
     * 
     * @param customerDTO
     * @return 
     */
    public static Customer customerDTOToEntity(CustomerDTO customerDTO){
        Customer customerEntity = new Customer();
        
        customerEntity.setId(customerDTO.getId());
        customerEntity.setFirstName(customerDTO.getFirstName());
        customerEntity.setSurName(customerDTO.getSurName());
        customerEntity.setNumberOfIdCard(customerDTO.getNumberOfIdCard());
        
        return customerEntity;
    }
    
    /**
     * Transfers entity to DTO object
     * 
     * @param hotelEntity
     * @return 
     */
    public static HotelDTO hotelEntityToDTO(Hotel hotelEntity){
        HotelDTO hotelDTO = new HotelDTO();
        
        hotelDTO.setId(hotelEntity.getId());
        hotelDTO.setName(hotelEntity.getName());
        hotelDTO.setAdress(hotelEntity.getAdress());
        hotelDTO.setCity(hotelEntity.getCity());
        hotelDTO.setDescription(hotelEntity.getDescription());
        
        List<RoomDTO> roomsDTO = new ArrayList<RoomDTO>();
        for(Room room: hotelEntity.getRooms()){
            roomsDTO.add(roomEntityToDTO(room));
        }
        
        hotelDTO.setRooms(roomsDTO);
        
        return hotelDTO;
    }
    
    /**
     * Transfers DTO to entity
     * 
     * @param hotelDTO
     * @return 
     */
    public static Hotel hotelDTOToEntity(HotelDTO hotelDTO){
        Hotel hotelEntity = new Hotel();
        
        hotelEntity.setId(hotelDTO.getId());
        hotelEntity.setName(hotelDTO.getName());
        hotelEntity.setAdress(hotelDTO.getAdress());
        hotelEntity.setCity(hotelDTO.getCity());
        hotelEntity.setDescription(hotelDTO.getDescription());
        if(hotelDTO.getRooms() == null)
        {
            hotelDTO.setRooms(new ArrayList<RoomDTO>());
        }
        List<Room> roomsEntities = new ArrayList<Room>();
        for(RoomDTO roomDTO: hotelDTO.getRooms()){
            roomsEntities.add(roomDTOToEntity(roomDTO));
        }        
        hotelEntity.setRooms(roomsEntities);
        
        return hotelEntity;
    }
    
    /**
     * Transfers entity to DTO object
     * 
     * @param reservationEntity
     * @return 
     */
    public static ReservationDTO reservationEntityToDTO(Reservation reservationEntity){
        ReservationDTO reservationDTO = new ReservationDTO();
        
        reservationDTO.setId(reservationEntity.getId());
        reservationDTO.setStartDate(reservationEntity.getStartDate());
        reservationDTO.setDuration(reservationEntity.getDuration());
        reservationDTO.setTotalPrice(reservationEntity.getTotalPrice());
        reservationDTO.setPaid(reservationEntity.isPaid());
        
        reservationDTO.setRoom(roomEntityToDTO(reservationEntity.getRoom()));
        reservationDTO.setHotel(hotelEntityToDTO(reservationEntity.getHotel()));
        
        List<CustomerDTO> customersDTO = new ArrayList<CustomerDTO>();
        for(Customer customerEntity: reservationEntity.getCustomers()){
            customersDTO.add(customerEntityToDTO(customerEntity));
        }        
        reservationDTO.setCustomers(customersDTO);  
        
        
        return reservationDTO;
    }
    
    /**
     * Transfers DTO to entity
     * 
     * @param reservationDTO
     * @return 
     */
    public static Reservation reservationDTOToEntity(ReservationDTO reservationDTO){
        Reservation reservationEntity = new Reservation();
        
        reservationEntity.setId(reservationDTO.getId());
        reservationEntity.setStartDate(reservationDTO.getStartDate());
        reservationEntity.setDuration(reservationDTO.getDuration());
        reservationEntity.setTotalPrice(reservationDTO.getTotalPrice());
        reservationEntity.setPaid(reservationDTO.isPaid());
        
        reservationEntity.setRoom(roomDTOToEntity(reservationDTO.getRoom()));
        reservationEntity.setHotel(hotelDTOToEntity(reservationDTO.getHotel()));
        
        List<Customer> customersEntities = new ArrayList<Customer>();
        for(CustomerDTO customerDTO: reservationDTO.getCustomers()){
            customersEntities.add(customerDTOToEntity(customerDTO));
        }        
        reservationEntity.setCustomers(customersEntities);
        
        
        return reservationEntity;
    }
    
    /**
     * Transfers entity to DTO object
     * 
     * @param roomEntity
     * @return 
     */
    public static RoomDTO roomEntityToDTO(Room roomEntity){
        RoomDTO roomDTO = new RoomDTO();
        
        roomDTO.setId(roomEntity.getId());
        roomDTO.setRoomNumber(roomEntity.getRoomNumber());
        roomDTO.setCapacity(roomEntity.getCapacity());
        roomDTO.setPrice(roomEntity.getPrice());
        roomDTO.setHotel(hotelEntityToDTO(roomEntity.getHotel()));
        
        return roomDTO;
        
    }
    
    /**
     * Transfers DTO to entity
     * 
     * @param roomDTO
     * @return 
     */
    public static Room roomDTOToEntity(RoomDTO roomDTO){
        Room roomEntity = new Room();
        
        roomEntity.setId(roomDTO.getId());
        roomEntity.setRoomNumber(roomDTO.getRoomNumber());
        roomEntity.setCapacity(roomDTO.getCapacity());
        roomEntity.setPrice(roomDTO.getPrice());
        roomEntity.setHotel(hotelDTOToEntity(roomDTO.getHotel()));
        
        return roomEntity;
    }
    
    /**
     * Transfers entity to DTO object
     * 
     * @param administratorEntity
     * @return 
     */
    public static AdministratorDTO administratorEntityToDTO(Administrator administratorEntity){
        AdministratorDTO administratorDTO = new AdministratorDTO();
        
        administratorDTO.setId(administratorEntity.getId());
        administratorDTO.setFirstName(administratorEntity.getFirstName());
        administratorDTO.setSurName(administratorEntity.getSurName());
        administratorDTO.setUsername(administratorEntity.getUsername());
        administratorDTO.setPassword(administratorEntity.getPassword());
        
        List<AdminRoleDTO> rolesDTO = new ArrayList<AdminRoleDTO>();
        for(AdminRole a: administratorEntity.getRoles())
        {
            AdminRoleDTO adminRoleDTO = new AdminRoleDTO();
            adminRoleDTO.setId(a.getId());
            adminRoleDTO.setUser(administratorDTO);
            adminRoleDTO.setPermission(a.getRoles());
            rolesDTO.add(adminRoleDTO);
            
        }
        administratorDTO.setRoles(rolesDTO);
        
        
        return administratorDTO;
    }
    
    /**
     * 
     * @param administratorDTO
     * @return 
     */
    public static Administrator administratorDTOToEntity(AdministratorDTO administratorDTO){
        Administrator administratorEntity = new Administrator();
        
        administratorEntity.setId(administratorDTO.getId());
        administratorEntity.setFirstName(administratorDTO.getFirstName());
        administratorEntity.setSurName(administratorDTO.getSurName());
        administratorEntity.setUsername(administratorDTO.getUsername());
        administratorEntity.setPassword(administratorDTO.getPassword());
        List<AdminRole> roles = new ArrayList<AdminRole>();
        for(AdminRoleDTO a: administratorDTO.getRoles())
        {
            AdminRole adminRole = new AdminRole();
            adminRole.setId(a.getId());
            adminRole.setRoles(a.getPermission());
            adminRole.setUser(administratorEntity);
            roles.add(adminRole);
            
        }
        administratorEntity.setRoles(roles);
        
        return administratorEntity;
    }
    
}
